package com.malou.client.reportes;

import java.util.HashMap;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.malou.client.MalouConstants;

public class GReporteInventarioPanel extends ContentPanel {	
	Button print;
	Button excel;
	Button mail;
	Status status;
	Status status_xls;	
	ContentPanel hidePanel;
	EnviarCorreoDialog sendCorreo;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public GReporteInventarioPanel(){
		setLayout(new FitLayout());
		setFrame(false);		
		setIcon(IconHelper.createPath("images/icons/table.png"));  
        setHeading(constants.reporteinventario());            
        
        ToolBar toolBar = new ToolBar();
                
        status = new Status();
	    status.setBusy(constants.cargando());
	    status.hide();    
	    status.setAutoWidth(true);
	    status.setStyleAttribute("paddingLeft", "20px");
	    toolBar.add(status);             
        
        print = new Button(constants.imprimir(), IconHelper.createPath("images/icons/print.png"));        
        print.setEnabled(false);
                
        toolBar.add(print);
        
        status_xls = new Status();
        status_xls.setBusy(constants.cargando());
        status_xls.hide();    
        status_xls.setAutoWidth(true);
        status_xls.setStyleAttribute("paddingLeft", "20px");
        toolBar.add(status_xls);
        
        excel = new Button(constants.exportToXls(), IconHelper.createPath("images/icons/excel.png"));
        excel.setStyleAttribute("paddingLeft", "20px");        
        
        SelectionListener<ButtonEvent> sl_excel;        
        sl_excel = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {				
				status_xls.show();
				excel.hide();
				hidePanel.removeAll();
				
				HashMap<String, Object> param = new HashMap<String, Object>();
												
				XlsReportDownload reportViewer = new XlsReportDownload("reports/g_reporte_inventario", param, status_xls, excel, null);
									
				hidePanel.add(reportViewer);
				hidePanel.layout();
			}
        };
        excel.addSelectionListener(sl_excel);
        
        toolBar.add(excel);
        
        mail = new Button(constants.enviarCorreo(), IconHelper.createPath("images/icons/mail.png"));
        mail.setStyleAttribute("paddingLeft", "20px");        
        
        SelectionListener<ButtonEvent> sl_mail;
        sendCorreo = new EnviarCorreoDialog();
        sl_mail = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {				
				sendCorreo.reset();
				sendCorreo.rptGInventario = GReporteInventarioPanel.this;
				sendCorreo.show();
				
			}
        };
        mail.addSelectionListener(sl_mail);
        
        toolBar.add(mail);
        
        setTopComponent(toolBar);
        
        if(hidePanel!= null)
        hidePanel.removeFromParent();
        hidePanel= new ContentPanel();        
        hidePanel.setVisible(false);        
        RootPanel.get().add(hidePanel);        
        loadReport();
	}	
	
	public void enviarEmail(EnviarCorreo send){
		send.setSubject(constants.reporteinventario());
		
		HashMap<String, Object> param = new HashMap<String, Object>();			
		
		Reportes.correoDialog = sendCorreo;
		Reportes.enviarEmail("reports/g_reporte_inventario", param, send,null);
	}
	
	private void loadReport(){
		status.show();				
		HashMap<String, Object> param = new HashMap<String, Object>();										
		PdfReportViewer reportViewer = new PdfReportViewer("reports/g_reporte_inventario", param, status,null,print, null);		
		add(reportViewer);		
	}
}
